<template>
  <el-main class="subject">
    <div>
      <!-- Form -->
      <el-button
        type="primary"
        @click="dialogFormVisible = !dialogFormVisible"
        round
        class="primaryButton"
        style="margin-right: 10px;width:180px"
        >添加晚归学生信息
      </el-button>

      <el-dialog
        title="添加晚归学生信息"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form :model="lateForm">
          <el-form-item label="楼房号" :label-width="formLabelWidth">
            <el-select v-model="lateForm.housId" placeholder="请选择楼房号" filterable clearable>
            <el-option
              v-for="item in housIdList"
              :key="item.housId"
              :value="item.housId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="宿舍号" :label-width="formLabelWidth">
            <el-select v-model="lateForm.dormId" placeholder="请选择宿舍号" filterable clearable>
            <el-option
              v-for="item in dormIdList"
              :key="item.dormId"
              :value="item.dormId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="学号" :label-width="formLabelWidth">
           <el-select v-model="lateForm.stuId" placeholder="请选择学号" filterable clearable>
            <el-option
              v-for="item in studentIdList"
              :key="item.stuId"
              :value="item.stuId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="晚归时间" :label-width="formLabelWidth">
           <el-date-picker
              v-model="lateForm.lateDate"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="值班人员" :label-width="formLabelWidth">
             <el-select v-model="lateForm.lateWatch" placeholder="请选择值班人员" filterable clearable>
              <el-option
                v-for="item in empNameList"
                :key="item.empId"
                :value="item.empName">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="晚归备注" :label-width="formLabelWidth">
            <el-input
              v-model="lateForm.lateDes"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addLate()">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="修改晚归学生信息"
        :visible.sync="dialogFormVisibleByUpdate"
        width="500px"
      >
         <el-form :model="lateForm">
          <el-form-item label="楼房号" :label-width="formLabelWidth">
            <el-select v-model="lateForm.housId" placeholder="请选择楼房号" filterable clearable>
            <el-option
              v-for="item in housIdList"
              :key="item.housId"
              :value="item.housId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="宿舍号" :label-width="formLabelWidth">
            <el-select v-model="lateForm.dormId" placeholder="请选择宿舍号" filterable clearable>
            <el-option
              v-for="item in dormIdList"
              :key="item.dormId"
              :value="item.dormId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="学号" :label-width="formLabelWidth">
           <el-select v-model="lateForm.stuId" placeholder="请选择学号" filterable clearable>
            <el-option
              v-for="item in studentIdList"
              :key="item.stuId"
              :value="item.stuId">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="晚归时间" :label-width="formLabelWidth">
           <el-date-picker
              v-model="lateForm.lateDate"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="值班人员" :label-width="formLabelWidth">
             <el-select v-model="lateForm.lateWatch" placeholder="请选择值班人员" filterable clearable>
              <el-option
                v-for="item in empNameList"
                :key="item.empId"
                :value="item.empName">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="晚归备注" :label-width="formLabelWidth">
            <el-input
              v-model="lateForm.lateDes"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateLate()">确 定</el-button>
        </div>
      </el-dialog>

      <el-button
        type="primary"
        round
        class="primaryButton"
        style="width: 186px"
        @click="getAllLate()"
        >查询全部晚归学生信息</el-button
      >

    
       <el-input
        placeholder="
         学号
        "
        v-model="findKeyBySId"
        style="width: 120px; margin-left: 30%"
        clearable
      >
      </el-input>
       <el-input
        placeholder="
         楼房号
        "
        v-model="findKeyByHId"
        style="width: 120px; margin-left: 1%"
        clearable
      >
      </el-input>
      <el-input
        placeholder="
         宿舍号
        "
        v-model="findKeyByDId"
        style="width: 120px; margin-left: 1%"
        clearable
      >
      </el-input>
      <el-button
        icon="el-icon-search"
        circle
        type="primary"
        style="margin-left: 10px"
        @click="getLateById()"
      ></el-button>
    </div>

    <div style="margin-top: 30px">
      <el-table
        :data="lateTableData"
        border
        style="width: 1200px"
        v-loading="loading"
        tooltip-effect="dark"
        @selection-change="selectionChange($event)"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="lateId" label="晚归号" width="150">
        </el-table-column>
        <el-table-column prop="housId" label="楼房号" width="120">
        </el-table-column>
        <el-table-column prop="dormId" label="宿舍号" width="120">
        </el-table-column>
        <el-table-column prop="stuId" label="学号" width="120">
        </el-table-column>
        <el-table-column prop="lateDate" label="晚归时间" width="120"  :formatter="formatDate">
        </el-table-column>
        <el-table-column prop="lateWatch" label="值班人员" width="120">
        </el-table-column>
        <el-table-column prop="lateDes" label="晚归备注" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              @click="dialogFormVisibleShow(scope.row)"
              type="primary"
              circle
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              circle
              size="small"
              @click="deleteLate(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px; margin-left: 20%">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.pageTotal"
      >
      </el-pagination>
    </div>
  </el-main>
</template>

<script>
import { Loading } from "element-ui";
export default {
  name: "Late",
  data() {
    return {
      lateTableData: [],
      findKeyByHId: "",
      findKeyByDId:"",
      findKeyBySId: "",
      dialogFormVisible: false,
      dialogFormVisibleByUpdate: false,
      loading: true,
      multipleSelection: [],
      //获得所有的学生ID，放到选项框中，这样避免输错导致存不到数据库
      studentIdList:[],
      dormIdList:[],
      housIdList:[],
      empNameList:[],
      // idListValue:{
      //   IdValue:""
      // },
      // late表单
      lateForm: {
        lateId: "",
        housId: "",
        dormId: "",
        stuId: "",
        lateDate: "",
        lateWatch: "",
        lateDes: "",
      },
      formLabelWidth: "130px",
      //匹配对应的方法，1是查询全部，2是查询单个
      eqMethod: {
        methodNum: 0,
      },
      //传输晚归学生信息数据的默认参数
      late: {
        lateId: "0",
        housId: "0",
        dormId: "0",
        stuId: "0"
      },
      //传输page数据的默认参数
      page: {
        currentPage: 1,
        sizePage: 5,
        pageTotal: 0,
        pageSizes: [3, 5, 8],
      },
      // 传输数据的容器
      objectData: {
        late: {},
        page: {},
      },
    };
  },
  methods: {
    formatDate(row, column){
  // 获取单元格数据
          let data = row[column.property]
          if(data == null) {
              return null
          }
          let dt = new Date(data)
          return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
    },
    handleClick(a) {
      console.log(a);
    },
    //选择一次展现几个时候触发
    handleSizeChange(size) {
      console.log("handleSizeChange变换");
      this.page.sizePage = size;
      // this.getAllLate();
      if (this.eqMethod.methodNum === 1) {
        this.getAllLate();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getLateById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getStudentByName();
      }
      console.log("this.page.sizePage" + this.page.sizePage);
    },
    //当前页变化时候触发
    handleCurrentChange(currentPageNum) {
      this.page.currentPage = currentPageNum;
      if (this.eqMethod.methodNum === 1) {
        this.getAllLate();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getLateById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getStudentByName();
      }
      // console.log(this.eqMethod.methodNum===1);
      // this.getAllLate();
    },
    // 获得全部晚归学生信息
    getAllLate() {
      this.eqMethod.methodNum = 1;
      console.log(
        this.$globalConfig.testUrl + "late/getAllLate/" + this.objectData
      );
      this.objectData.late = this.late;
      this.objectData.page = this.page;
      console.log(this.objectData);
      this.$axios
        .post(this.$globalConfig.testUrl + "late/getAllLate/", {
          page: this.page,
        })
        .then(
          (response) => {
            console.log("请求成功了", response.data);
            this.lateTableData = response.data.extend.lateList;
            this.page.currentPage4 = response.data.extend.currentPage;
            this.page.sizePage = response.data.extend.sizePage;
            this.page.pageTotal = response.data.extend.pageTotal;
            this.loading = false;
          },
          (error) => {
            console.log("请求失败了", error.message);
            this.loading = false;
          }
        );
    },
    //按照条件来查询
    getLateById() {
      this.eqMethod.methodNum = 2;
      this.late.housId = this.findKeyByHId ;
      this.late.dormId = this.findKeyByDId;
      this.late.stuId = this.findKeyBySId;
      console.log({
        object: this.late,
        page: this.page,
      });
      if (this.late.lateId != "") {
        this.$axios
          .post(this.$globalConfig.testUrl + "late/getOneLate", {
            object: this.late,
            page: this.page,
          })
          .then(
            (response) => {
              console.log("请求成功了", response.data);
              this.lateTableData = response.data.extend.lateList;
              this.page.currentPage4 = response.data.extend.currentPage;
              this.page.sizePage = response.data.extend.sizePage;
              this.page.pageTotal = response.data.extend.pageTotal;
              this.loading = false;
            },
            (error) => {
              console.log("请求失败了", error.message);
              this.loading = false;
            }
          );
      } else {
        alert("您输入的学号是空");
      }
    },
    
    //添加晚归学生信息
    addLate() {
       this.verifyUser();
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "late/addLate",
        data: this.lateForm,
      }).then(
        (response) => {
          console.log("请求成功了 addLate", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllLate();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.dialogFormVisible = false;
    },

    //删除晚归学生信息
    deleteLate(late) {
       this.verifyUser();
      console.log(late.lateId);
      // this.late.lateId = late.lateId;
      // 弹窗提示框
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if(sessionStorage.getItem("rid")==="1"){
          this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "late/deleteLate",
            data: { dormId:late.dormId,housId:late.housId,stuId:late.stuId,lateId:late.lateId },
          }).then(
            (response) => {
              if (response.data.msg === "处理成功")
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              console.log("请求成功了 deleteLate", response.data);
              this.getAllLate();
            },
            (error) => {
              console.log("请求失败了", error.message);
            }
          );}
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });

      // 将数据恢复原样
      this.late.lateId = "";
    },
    // 展示对话框
    dialogFormVisibleShow(late) {
      this.lateForm = late;
      this.dialogFormVisibleByUpdate = true;
    },
    // 修改晚归学生信息
    updateLate() {
       this.verifyUser();
      console.log(this.lateForm);
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "late/updateLate",
        data: this.lateForm,
      }).then(
        (response) => {
          console.log("请求成功了updateLate", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllLate();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.dialogFormVisibleByUpdate = false;
    },
    verifyUser(){
     this.$axios
          .post(this.$globalConfig.testUrl + "admin/verifyUser/", {
          username:sessionStorage.getItem("username"),password:sessionStorage.getItem("password"),rid:sessionStorage.getItem("rid")
          })
          .then(
          (response) => {
              console.log("请求成功了", response.data);
              if(response.data.code === 200){
                  return "ok";
              }else{
                  alert("会话出现问题，请重新登录！");
                  sessionStorage.removeItem('rid');
                  this.$router.push({
                      name:'logout'
                  });
              }

          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
      
    },
    getAllStudentId(){
       this.$axios
          .post(this.$globalConfig.testUrl + "student/getAllStudentId")
          .then(
          (response) => {
              this.studentIdList = response.data.extend.stuIdList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
    getAllDromHId(){
          this.$axios
            .post(this.$globalConfig.testUrl + "dorm/getAllDromHId",{"dormId":this.lateForm.dormId})
            .then(
            (response) => {
                console.log("getAllDHIdList 请求成功了", response.data);
                this.housIdList = response.data.extend.HIdList;
            },
            (error) => {
                console.log("请求失败了", error.message);
            }
        );
      
      
    },
    getAllDromDId(){
      this.$axios
          .post(this.$globalConfig.testUrl + "dorm/getAllDromDId",{"housId":this.lateForm.housId})
          .then(
          (response) => {
              console.log("getAllDHIdList 请求成功了", response.data);
              this.dormIdList = response.data.extend.DIdList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
    getAllEmpName(){
      this.$axios
          .post(this.$globalConfig.testUrl + "emp/getEmpNameList")
          .then(
          (response) => {
              console.log("getAllEmpName 请求成功了", response.data);
              this.empNameList = response.data.extend.empNameList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
   
    // 这里可以做多个删除
    selectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((a)=>{
        console.log(a.lateId);
      })
    },
  },
  watch:{
      lateForm:{
					deep:true,
					handler(){
            this.getAllStudentId();
            this.getAllDromHId();
						this.getAllDromDId();
					}
				}
  },
  mounted() {
    this.getAllLate();
    this.getAllStudentId();
    this.getAllDromDId();
    this.getAllDromHId();
    this.getAllEmpName();
    // console.log("Student被挂载了")
    // this.$bus.$emit('ControlHomeSpan',false);
  },
  beforeDestroy() {
    // console.log("Main被摧毁了")
  },
};
</script>

<style scoped>
.subject {
  position: absolute;
  top: 100px;
  left: 40px;
  flex: auto;
  background-color: white;
}
.primaryButton {
  width: 100px;
  height: 40px;
  font-size: 16px;
}
.inputSize {
  width: 200px;
}
</style>